package com.hzit.servlet;

import cn.hutool.core.util.StrUtil;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.hzit.dto.StudentDTO;
import com.hzit.entity.PageResult;
import com.hzit.entity.R;
import com.hzit.service.IStudentService;
import com.hzit.util.ResponseUtil;
import com.hzit.vo.StudentVo;
import org.springframework.web.context.WebApplicationContext;
import org.springframework.web.context.support.WebApplicationContextUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

@WebServlet("/student")
public class StudentServlet extends HttpServlet {

	private IStudentService studentService;

	@Override
	public void init() throws ServletException {
		WebApplicationContext wac = WebApplicationContextUtils.getWebApplicationContext(this.getServletContext());
		studentService = wac.getBean(IStudentService.class);
	}

	@Override
	protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		resp.setContentType("application/json;charset=utf8");
		//1. 得到请求参数
		String cmd = req.getParameter("cmd");
		//2. 根据参数调用不同的方法
		if(StrUtil.isNotBlank(cmd)){        // 相当于： cmd != null && !cmd.equals("")
			if("list".equals(cmd)){         // 查询学生列表
				list(req,resp);
			}
			if("search".equals(cmd)){       // 分页带条件查询
				search(req,resp);
			}
			if("add".equals(cmd)){          // 添加学生
				add(req,resp);
			}
			if("update".equals(cmd)){          // 修改学生
				update(req,resp);
			}
			if("delete".equals(cmd)){       // 删除学生
				delete(req,resp);
			}
		}
	}

	private void delete(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到要删除的学生编号
		String sids = req.getParameter("sids");     //格式为：1,2,3
		try {
			studentService.delete(sids);
			ResponseUtil.out(resp,R.ok(null,"删除学生成功"));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("删除学生失败"));
		}

	}

	private void update(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//1. 得到前台发来的参数
		ObjectMapper mapper = new ObjectMapper();
		StudentDTO dto = mapper.readValue(req.getInputStream(), StudentDTO.class);
		try {
			//2. 修改到数据库中
			studentService.update(dto);
			//3. 输出
			ResponseUtil.out(resp,R.ok(null,"修改学生成功"));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("修改学生失败"));
		}
	}

	private void add(HttpServletRequest req, HttpServletResponse resp)  throws ServletException, IOException {
		//1. 得到前台发来的参数
		ObjectMapper mapper = new ObjectMapper();
		StudentDTO dto = mapper.readValue(req.getInputStream(), StudentDTO.class);
		try {
			//2. 添加到数据库中
			studentService.add(dto);
			//3. 输出
			ResponseUtil.out(resp,R.ok(null,"添加学生成功"));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("添加学生失败"));
		}
	}

	private void search(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 得到分页参数
		int page = Integer.parseInt(req.getParameter("page"));
		int pageSize = Integer.parseInt(req.getParameter("pageSize"));
		//2. 得到条件查询对象
		ObjectMapper mapper = new ObjectMapper();
		StudentVo studentVo = mapper.readValue(req.getInputStream(), StudentVo.class);
		try {
			//3. 开始条件带分页查询
			PageResult<StudentDTO> pr = studentService.search(page,pageSize,studentVo);
			//4. 返回到前端
			ResponseUtil.out(resp,R.ok(pr));
		} catch (Exception e) {
			e.printStackTrace();
			ResponseUtil.out(resp,R.fail("条件查询失败，原因是：" + e.getMessage()));
		}
	}

	private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		//1. 查询所有的学生列表
		List<StudentDTO> studentDTOList = studentService.findAll();
		//2. 输出到前端
		ResponseUtil.out(resp, R.ok(studentDTOList));
	}
}
